package com.huyun.web.FutureTest;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @program:
 * @description:
 * @author: hyq
 * @create: 2021-12-30 10:15
 */
public class AcquireResultTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //getNow方法测试
        CompletableFuture<String> cp1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(60 * 1000 * 60 );
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return "hello world";
        });

        System.out.println(cp1.getNow("hello h2t"));
//
//        //join方法测试 join方法获取结果方法里不会抛异常，但是执行结果会抛异常，抛出的异常为CompletionException
        CompletableFuture<Integer> cp2 = CompletableFuture.supplyAsync((()-> 1 / 0));
        System.out.println(cp2.join());
//
//        //get方法测试 get方法获取结果方法里将抛出异常，执行结果抛出的异常为ExecutionException
        CompletableFuture<Integer> cp3 = CompletableFuture.supplyAsync((()-> 1 / 1));
        System.out.println(cp3.get());

//        CompletableFuture completableFuture = new CompletableFuture();
//        new Thread(() -> {
//            try {
//                //doSomething，调用complete方法将其他方法的执行结果记录在completableFuture对象中
//                completableFuture.complete(null);
//            } catch (Exception e) {
//                //异常处理
//                completableFuture.completeExceptionally(e);
//            }
//        }).start();

    }
}
